﻿using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour 
{
    public float velocityX = 4;
    public float velocityY = 4;
    public float gravity = 0.5f;
    public float heightJump = -6.0f;
    public bool onGround = false;

    Vector2 newPosition;
    

//     private Transform groundCheck;
//     bool grounded = false;

    void Start()
    {
        /*groundCheck = transform.Find("groundCheck");*/
    }

    void Update()
    {
        /*grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));*/

        if (Input.GetButton("left"))
            newPosition.x -= velocityX * Time.deltaTime;
        else if (Input.GetButton("right"))
            newPosition.x += velocityX * Time.deltaTime;


        if (Input.GetButton("Jump"))
        {
            if (onGround)
            {
                velocityY = -12f;
                onGround = false;
            }
        }
        else
        {
            if (velocityY < heightJump)
                velocityY = heightJump;
        }

        if (!onGround)
        {
            velocityY += gravity;
            newPosition.y -= velocityY * Time.deltaTime;
        }

        transform.position = newPosition;
    }


    void OnTriggerEnter2D(Collider2D collidedObject)
    {
        if (collidedObject.tag == "platform")
        {
            print("platform");
            newPosition.y = collidedObject.transform.position.y + 0.1634627f;
            transform.position = newPosition;
            velocityY = 0.0f;
            onGround = true;
        }
    }
}
